package org.dromara.demo.controller;

import org.dromara.demo.domain.BusiForm;
import org.dromara.demo.domain.CargoItem;
import org.dromara.demo.domain.Contact;
import org.dromara.demo.domain.dto.BusiFormDto;
import org.dromara.demo.domain.dto.BusiFormSubmitDto;
import org.dromara.demo.service.IBusiFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务表单控制器
 * 处理业务表单相关的HTTP请求
 */
@RestController
@RequestMapping("/demo/busi-forms")
@Validated
public class BusiFormController {

    @Autowired
    private IBusiFormService busiFormService;

    /**
     * 提交业务表单（与前端表单结构对应）
     *
     * @param busiFormSubmitDto 业务表单提交数据传输对象
     * @return 保存的业务表单实体对象
     */
    @PostMapping("/submit")
    public ResponseEntity<BusiForm> submitBusiFormFromFrontend(@RequestBody @Validated BusiFormSubmitDto busiFormSubmitDto) {
        BusiForm savedBusiForm = busiFormService.submitBusiFormFromFrontend(busiFormSubmitDto);
        return ResponseEntity.ok(savedBusiForm);
    }

    /**
     * 提交业务表单
     *
     * @param busiFormDto 业务表单数据传输对象
     * @return 保存的业务表单实体对象
     */
    @PostMapping
    public ResponseEntity<BusiForm> submitBusiForm(@RequestBody BusiFormDto busiFormDto) {
        BusiForm savedBusiForm = busiFormService.submitBusiForm(busiFormDto);
        return ResponseEntity.ok(savedBusiForm);
    }

    /**
     * 根据ID获取业务表单
     *
     * @param id 业务表单ID
     * @return 业务表单实体对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<BusiForm> getBusiFormById(@PathVariable Long id) {
        BusiForm busiForm = busiFormService.getBusiFormById(id);
        if (busiForm != null) {
            return ResponseEntity.ok(busiForm);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 获取所有业务表单
     *
     * @return 业务表单实体对象列表
     */
    @GetMapping
    public ResponseEntity<List<BusiForm>> getAllBusiForms() {
        List<BusiForm> busiForms = busiFormService.getAllBusiForms();
        return ResponseEntity.ok(busiForms);
    }

    /**
     * 根据业务表单ID获取货物明细列表
     *
     * @param busiFormId 业务表单ID
     * @return 货物明细实体对象列表
     */
    @GetMapping("/{id}/cargo-items")
    public ResponseEntity<List<CargoItem>> getCargoItemsByBusiFormId(@PathVariable("id") Long busiFormId) {
        List<CargoItem> cargoItems = busiFormService.getCargoItemsByBusiFormId(busiFormId);
        return ResponseEntity.ok(cargoItems);
    }

    /**
     * 根据业务表单ID获取联系人列表
     *
     * @param busiFormId 业务表单ID
     * @return 联系人实体对象列表
     */
    @GetMapping("/{id}/contacts")
    public ResponseEntity<List<Contact>> getContactsByBusiFormId(@PathVariable("id") Long busiFormId) {
        List<Contact> contacts = busiFormService.getContactsByBusiFormId(busiFormId);
        return ResponseEntity.ok(contacts);
    }
}